package com.project.restaurant.order.dao;

import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.project.restaurant.order.dto.BriefOrder;
import com.project.restaurant.order.dto.OrderQueryParam;
import com.project.restaurant.order.typehandler.OrderStateTypeHandler;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BriefOrderDao {

    @Results(id = "BriefOrderResultMap",value = {
            @Result(column = "order_id",property = "orderId"),
            @Result(column = "order_state",property = "orderState",typeHandler = OrderStateTypeHandler.class),
            @Result(column = "order_id",property = "orderItems",
                    many = @Many(select = "com.project.restaurant.order.dao.OrderItemDao.selectByOrderId"))
    })
    @Select("select o.order_id,o.total_price,o.order_state,o.order_purchase_time " +
            "from `order` o " +
            "where o.order_id = #{orderId}")
    BriefOrder selectById(String orderId);

    @ResultMap("BriefOrderResultMap")
    @Select("<script>" +
            "select o.order_id,o.total_price,o.order_state,o.order_purchase_time " +
            "from `order` o " +
            "where " +
            "o.user_id = #{ew.userId} and " +
            "<if test='ew.orderState != null'>" +
                "o.order_state = #{ew.orderState.orderState} and " +
            "</if>" +
            "1 " +
            "ORDER BY o.order_purchase_time DESC " +
            "</script>")
    List<BriefOrder> selectByUserId(@Param(Constants.WRAPPER)OrderQueryParam queryParam);
}
